﻿//radioon = false;
//hostname = "http://127.0.0.1:8080/";
//musiclist store the info of musicid,name,singer,album,link
var musiclist = new Array();
var musicinfo = new Array();
var curmusicindex = 0;

/*
$(document).ready(loadok);

function loadok(){
    radioon = false;
    $("#radio_on_off").click(radio_on_off);
    $("#audioele").attr("onended","nextmusic()");
    $("#audioele").attr("ontimeupdate","musictimeupdate()");
}
*/

function radio_on_off(){
    //first check whether the radio is on or off
    if(radioon){
        //turn off the radio
        //empty all element
        radioon = false;
        //$("#audioele").attr("src","");
        aa = document.getElementById("audioele")
        aa.pause();
        $("#musicplayer").hide();
        $("#lyricshow").hide();
        $("#showpicture").hide();
        $("#annoation").hide();
    }
    else
    {
        //init the face
        //get musiclist
        //get lyric corresponde to the music
        //play the mucis
        try
        {
            radioon = true;
            
            //call next music?
            
            $("#musicplayer").show();
            $("#lyricshow").show();
            $("#showpicture").show();
            $("#annoation").show();        
            $.getJSON(appurlprefix+"/music/getmusic",getonemusic);
            
            //alert(curmusicindex);
            //alert(musiclist);
            //alert(musiclist[curmusicindex]);
            //var tmplink = musiclist[curmusicindex].detail.link;
            //$("#audioele").attr("src",tmplink);
        }
        catch(e)
        {
            $("#lyricshow").html(e.message+" turn on radio error");
        }
        finally
        {
        }
    }
}

/**
 * get the current music infomation
 * @param data json format data from server
 */
function getonemusic(data)
{
	//musicinfo = new Array();
	musicinfo[0] = data.key;
	musicinfo[1] = data.name;
	musicinfo[2] = data.album;
	musicinfo[3] = data.singer;
	musicinfo[4] = data.link;
	musicinfo[5] = data.pvalue;
	musicinfo[6] = data.avalue;
	musicinfo[7] = data.dvalue;
	createCookie("key",data.key);
	createCookie("name",data.name);
	createCookie("album",data.album);
	createCookie("singer",data.singer);
	//createCookie("link",data.link);
	//createCookie("pvalue",data.pvalue);
	//createCookie("avalue",data.avalue);
	//createCookie("dvalue",data.dvalue);	
	/*
	musicinfo['key'] = data.key;
	musicinfo['name'] = data.name;
	musicinfo['album'] = data.album;
	musicinfo['singer'] = data.singer;
	musicinfo['link'] = data.link;
	musicinfo['pvalue'] = data.pvalue;
	musicinfo['avalue'] = data.avalue;
	musicinfo['dvalue'] = data.dvalue;
	*/

	//check the support of html5
	var support = !!document.createElement('video').canPlayType;
	if(support)
	{
		$("#audioele").attr("src",musicinfo[4]);
  	$("#audioele").attr("autoplay","true");
  }
  else
  {
  	//如果有musicplayer，就对musicplayer操作
  	//否则就应该有test4
  	try
  	{
  		removeSWF("musicplayer");
  	}
  	catch(e)
  	{
  	}
  	
  	$("#musicplayer").html("");
      <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 
      var swfVersionStr = "10.0.0";
      <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
      var xiSwfUrlStr = "/static/swf/playerProductInstall.swf";
      var flashvars = {};
      var params = {};
      params.quality = "high";
      params.bgcolor = "#ffffff";
      params.allowscriptaccess = "sameDomain";
      params.allowfullscreen = "true";
      var attributes = {};
      attributes.id = "musicplayer";
      attributes.name = "test4";
      attributes.align = "middle";
      swfobject.embedSWF(
          "/static/swf/test4.swf?musicurl="+musicinfo[4],  "musicplayer", 
          "392", "164", 
          swfVersionStr, xiSwfUrlStr, 
          flashvars, params, attributes);
      swfobject.createCSS("#musicplayer", "display:block;text-align:left;");
	}
	
  //$("#musicinfo").html("歌曲名："+musicinfo['name']+"<br/>歌手："+musicinfo['singer']+"<br/>专辑："+musicinfo['album']);
  $("#musicinfo").html("歌曲名："+musicinfo[1]+" 歌手："+musicinfo[3]+" 专辑："+musicinfo[2]);
  
  if(musicinfo[5] != -1)
  {
  	$("#pm").html(musicinfo[5]);
  }
  else{
  	$("#pm").html("未标注");
  }
  
  if(musicinfo[6] != -1)
  {
  	$("#am").html(musicinfo[6]);
  }
  else{
  	$("#am").html("未标注");
  }
  
  if(musicinfo[7] != -1)
  {
  	$("#dm").html(musicinfo[7]);
  }
  else{
  	$("#dm").html("未标注");
  }
  
  $("#uploadmusicid").attr("value",musicinfo[0]);
  
  $("#lyricshow").html("<div>歌词加载中<br/><img src=\"/static/pic/loading.gif\"/></div>");
  $.getJSON(appurlprefix+"/music/getlyric?musicid="+musicinfo[0],getmusiclyric);
}

/**
 *
 * @param data json format data from server
 */
function getmusiclist(data){
    for(var i = 0;i < data.length;i++){
        musiclist.push(data[i]);
    }
    curmusicindex = 0;
    var tmplink = musiclist[curmusicindex].detail.link;
    $("#audioele").attr("src",tmplink);
    $("#audioele").attr("autoplay","true");
    $("#musicinfo").html("歌曲名："+musiclist[curmusicindex].detail.songname+"<br/>歌手："+musiclist[curmusicindex].detail.singer+"<br/>专辑："+musiclist[curmusicindex].detail.album);
    
    //get the lyric info

    //get the picture info
}

/**
 * get next music to listen to
 */
function nextmusic()
{
	$.getJSON(appurlprefix+"/music/getmusic",getonemusic);
	
	/*
    alert("next music");
   //next music to listen to 
   curmusicindex = curmusicindex+1;
   var tmplink = musiclist[curmusicindex].detail.link;
   $("#audioele").attr("src",tmplink);
//   $("#audioele").attr("autoplay","true");
   $("#musicinfo").html("歌曲名："+musiclist[curmusicindex].detail.songname+"<br/>歌手："+musiclist[curmusicindex].detail.singer+"<br/>专辑："+musiclist[curmusicindex].detail.album);
   */
}

function musictimeupdate()
{
    //aa = document.getElementById("audioele")
    //$("#lyricshow").html(aa.currentTime);
    
}

/**
 * call back function for getlyric
 * @param data lyric info return from the database
 */
function getmusiclyric(data){
	//define some function to define the lyric
	//in order to show the lyric
 
	var l = data.lyric;
	//alert(l);
	//l = l.replace(/\[/g,"<br/>[");
	var s= "<textarea rows=\"10\" cols=\"100\">"+l+"</textarea>";
	$("#lyricshow").html(s);
}
